<template>
  <div class="activity app-container">
    <div class="card-box">
      <el-button
        type="primary"
        @click="
          type = 'add';
          visible = true;
        "
      >
        添加促销活动
      </el-button>
    </div>
    <div class="card-box">
      <el-table :data="activityGoodsStore.list">
        <el-table-column label="名称" prop="name" />
        <el-table-column label="开始时间" prop="start_time" />
        <el-table-column label="结束时间" prop="end_time" />
        <el-table-column label="状态" prop="status" />
      </el-table>
    </div>
    <ActivityForm v-model="visible" :current-id="currentId" :type="type" />
  </div>
</template>

<script lang="ts" setup>
import useActivityGoodsStoreHook from "@/store/modules/goods/activity";
import { useUserStoreHook } from "@/store/modules/user";
import ActivityForm from "./components/activityForm.vue";

const userStore = useUserStoreHook();

const activityGoodsStore = useActivityGoodsStoreHook();

const query = reactive({
  page: 1,
  size: 10,
  supplier: (userStore.supplierInfo as any).id,
});

const visible = ref(false);
const type = ref("add");
const currentId = ref("");

watch(visible, (val) => {
  if (!val) {
    activityGoodsStore.fetchActivityList(query);
  }
});

async function handleResetQueryForm() {
  query.page = 1;
  query.size = 10;
  await activityGoodsStore.fetchActivityList(query);
}

function toEdit(row: any) {
  type.value = "edit";
  currentId.value = row.id;
  visible.value = true;
}

onMounted(async () => {
  await activityGoodsStore.fetchActivityList(query);
});
</script>

<style lang="scss" scoped></style>
